#!/bin/bash
# Io Version of Postflight

# Remove any existing FCP preference files
"$3/bin/rm" -f ~/Library/Preferences/Final\ Cut\ Pro\ User\ Data/Final\ Cut\ Pro\ [0-9.]*\ Preferences
"$3/bin/rm" -f ~/Library/Preferences/Final\ Cut\ Pro\ User\ Data/Final\ Cut\ Pro\ [0-9.]*\ Prefs.fcset
"$3/bin/rm" -f ~/Library/Preferences/Final\ Cut\ Pro\ User\ Data/Final\ Cut\ Pro\ Obj\ Cache.fcmch
"$3/bin/rm" -f ~/Library/Preferences/Final\ Cut\ Pro\ User\ Data/Final\ Cut\ Pro\ Prof\ Cache.fcpch
"$3/bin/rm" -f ~/Library/Preferences/Final\ Cut\ Pro\ User\ Data/Final\ Cut\ Pro\ MOA\ Cache
"$3/bin/rm" -f ~/Library/Preferences/Final\ Cut\ Pro\ User\ Data/Final\ Cut\ Pro\ POA\ Cache

# Remove old User Presets and Control Panel folder from prior 2.0 installs if they even exist
if [ -d "$3/Applications/Kona ControlPanel" ]; then
	if [ -d "$3/Applications/Kona ControlPanel/User Presets" ]; then
		if [ ! -d "$3/Library/Application Support/AJA/Kona Presets" ]; then
			"$3/bin/mkdir"	-vp		"$3/Library/Application Support/AJA/Kona Presets"
		fi
		"$3/bin/mv"	-vf		"$3/Applications/Kona ControlPanel/User Presets/"* "$3/Library/Application Support/AJA/Kona Presets"
	fi
	"$3/bin/rm"		-vrf	"$3/Applications/Kona ControlPanel"
fi

#copy the version plist to disk
"$3/bin/cp" "$1/Contents/Resources/com.aja.driver.plist" "$3/Library/Preferences/"
/bin/chmod 664 "$3/Library/Preferences/com.aja.driver.plist"

ERR=0
OS_VERSION=$("$3/usr/bin/sw_vers" -productVersion | "$3/usr/bin/sed" -e 's/\.[0-9]*$//')
OS_MAJOR_VERSION=$("$3/bin/echo" "$OS_VERSION" | "$3/usr/bin/sed" -e 's/\.[0-9]*$//')
OS_MINOR_VERSION=$("$3/bin/echo" "$OS_VERSION" | "$3/usr/bin/sed" -e 's/^[0-9]*\.//')
PERMUTE_CM=""
PERMUTE_DD="dd1"

if [ -d "$3/private/tmp/aja" ]; then
	if [ $OS_MAJOR_VERSION -eq 10 ]; then
		if [ $OS_MINOR_VERSION -lt 7 ]; then
			PERMUTE_CM="cm0"
		else
			PERMUTE_CM="cm1"
		fi

		pushd "$3/private/tmp/aja"		> "/dev/null"
			FILES=$("$3/usr/bin/find" . -name "$PERMUTE_CM$PERMUTE_DD*.kext" | "$3/usr/bin/tr" '\n' ',' | "$3/usr/bin/sed" -e 's:\./::g' -e 's:,$::')
		popd		> "/dev/null"

		IFS=,
		for origFileName in $FILES; do
			newFileName=$("$3/bin/echo" "$origFileName" | "$3/usr/bin/sed" -e "s:$PERMUTE_CM$PERMUTE_DD\.::")
			#"$3/bin/echo"	"## NOTE:  About to move '$origFileName' from '$3/private/tmp/aja' to '$3/System/Library/Extensions' and rename it '$newFileName'"
			if [ -d "$3/System/Library/Extensions/$newFileName" ]; then
				"$3/bin/rm"		-vfR	"$3/System/Library/Extensions/$newFileName"
			fi
			"$3/bin/mv"		-vf		"$3/private/tmp/aja/$origFileName"		"$3/System/Library/Extensions/$newFileName";  ERR=$?
			if [ $ERR -ne 0 ]; then
				"$3/bin/echo"	"## ERROR:  Failed to move & rename '$origFileName' from '$3/private/tmp/aja' to '$3/System/Library/Extensions/$newFileName' -- error $ERR"
				exit $ERR
			#else
				#"$3/bin/echo"	"## NOTE:  Moved & renamed '$origFileName' from '$3/private/tmp/aja/' to '$3/System/Library/Extensions/$newFileName'"
			fi
		done

		if [ $OS_MINOR_VERSION -gt 6 ]; then
			#	On 10.7 or later, install our CMIO DAL plugins and our AJA assistant plist.
			#	Create the CMIO Plug-Ins *DAL folders, if they don't exist...
			if [ ! -d "$3/Library/CoreMediaIO/Plug-Ins/DAL" ]; then
				"$3/bin/mkdir"	-vp		"$3/Library/CoreMediaIO/Plug-Ins/DAL"
			fi
			if [ ! -d "$3/Library/CoreMediaIO/Plug-Ins/FCP-DAL" ]; then
				"$3/bin/mkdir"	-vp		"$3/Library/CoreMediaIO/Plug-Ins/FCP-DAL"
			fi

			if [ -d "$3/Library/CoreMediaIO/Plug-Ins/DAL/AJA.plugin" ]; then
				#	Overinstall:  remove our existing plugin...
				"$3/bin/rm"		-vfR	"$3/Library/CoreMediaIO/Plug-Ins/DAL/AJA.plugin"
			fi
			"$3/bin/mv"			-vf		"$3/private/tmp/aja/Library/CoreMediaIO/Plug-Ins/DAL/AJA.plugin"		"$3/Library/CoreMediaIO/Plug-Ins/DAL/";  ERR=$?
			if [ $ERR -ne 0 ]; then
				"$3/bin/echo"	"## ERROR:  Failed to move 'AJA.plugin' from '$3/private/tmp/aja/Library/CoreMediaIO/Plug-Ins/DAL' into '$3/Library/CoreMediaIO/Plug-Ins/DAL/' -- error $ERR"
				exit $ERR
			#else
				#"$3/bin/echo"	"## NOTE:  Moved 'AJA.plugin' from '$3/private/tmp/aja/Library/CoreMediaIO/Plug-Ins/DAL' into '$3/Library/CoreMediaIO/Plug-Ins/DAL/'"
			fi

			if [ -d "$3/Library/CoreMediaIO/Plug-Ins/FCP-DAL/AJA.plugin" ]; then
				#	Overinstall:  remove our existing plugin...
				"$3/bin/rm"		-vfR	"$3/Library/CoreMediaIO/Plug-Ins/FCP-DAL/AJA.plugin"
			fi
			"$3/bin/mv"			-vf		"$3/private/tmp/aja/Library/CoreMediaIO/Plug-Ins/FCP-DAL/AJA.plugin"	"$3/Library/CoreMediaIO/Plug-Ins/FCP-DAL/";  ERR=$?
			if [ $ERR -ne 0 ]; then
				"$3/bin/echo"	"## ERROR:  Failed to move 'AJA.plugin' from '$3/private/tmp/aja/Library/CoreMediaIO/Plug-Ins/FCP-DAL' into '$3/Library/CoreMediaIO/Plug-Ins/FCP-DAL/' -- error $ERR"
				exit $ERR
			#else
				#"$3/bin/echo"	"## NOTE:  Moved 'AJA.plugin' from '$3/private/tmp/aja/Library/CoreMediaIO/Plug-Ins/FCP-DAL' into '$3/Library/CoreMediaIO/Plug-Ins/FCP-DAL/'"
			fi

			if [ -f "$3/Library/LaunchDaemons/com.aja.cmio.ajaassistant.plist" ]; then
				#	Overinstall:  remove our existing plist...
				"$3/bin/rm"		-vf		"$3/Library/LaunchDaemons/com.aja.cmio.ajaassistant.plist"
			fi
			"$3/bin/mv"			-vf		"$3/private/tmp/aja/Library/LaunchDaemons/com.aja.cmio.ajaassistant.plist"	"$3/Library/LaunchDaemons/";  ERR=$?
			if [ $ERR -ne 0 ]; then
				"$3/bin/echo"	"## ERROR:  Failed to move 'com.aja.cmio.ajaassistant.plist' from '$3/private/tmp/aja/Library/LaunchDaemons' into '$3/Library/LaunchDaemons/' -- error $ERR"
				exit $ERR
			#else
				#"$3/bin/echo"	"## NOTE:  Moved 'com.aja.cmio.ajaassistant.plist' from '$3/private/tmp/aja/Library/LaunchDaemons' into '$3/Library/LaunchDaemons/'"
			fi
		fi
	else
		echo "## ERROR:  Expected major version number to be 10 -- OS_VERSION=$OS_VERSION, OS_MAJOR_VERSION=$OS_MAJOR_VERSION, OS_MINOR_VERSION=$OS_MINOR_VERSION"
		exit 2
	fi
else
	echo "## NOTE:  Folder '$3/private/tmp/aja' not found"
	exit 404
fi


# Force a cache miss on the kext cache
"$3/usr/bin/touch" $3/System/Library/Extensions
